﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Data;

namespace ReconUI
{
    public partial class RemitterMatchInvoice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
         
            if (!Page.IsPostBack)
            {
                
                DBFactory db = new DBFactory();
                DataTable dtInvoice = db.GetUnmatchedInvoicesForRemitter(((UserVO)Session["CurrentUserVO"]).customerId, ((UserVO)Session["CurrentUserVO"]).userId);
                dtInvoice.Columns.Add("ref_no_or_cheque_no", typeof(System.String));
                dtInvoice.Columns.Add("statement_unique_id", typeof(System.Int32));
                dtInvoice.Columns.Add("statement_amount", typeof(System.Double));
                Session[SessionStore.UnMatchedInvoiceListForRemitter] = dtInvoice;
                grdInvoice.DataSource = dtInvoice;
                grdInvoice.DataBind();
            }
        }

        protected void btnMarkPayment_Click(object sender, EventArgs e)
        {
            DataTable dtInvoice = (DataTable)Session[SessionStore.UnMatchedInvoiceListForRemitter];
            if (dtInvoice != null)
            {
                string paymentReferenceNumber = txtPaymentReferenceNumber.Text;
                DBFactory db = new DBFactory();
                DataTable dtStatementDetails = db.GetStatementDetailsForRemitter(((UserVO)Session["CurrentUserVO"]).customerId, paymentReferenceNumber);
                if(dtStatementDetails!=null && dtStatementDetails.Rows.Count>0)
                {
                    foreach (GridViewRow invoiceRow in grdInvoice.Rows)
                    {
                        CheckBox chkSelect = (CheckBox)invoiceRow.Cells[0].FindControl("chkSelect");
                        if (chkSelect.Checked)
                        {
                            dtInvoice.Rows[invoiceRow.RowIndex][9] = paymentReferenceNumber;
                            dtInvoice.Rows[invoiceRow.RowIndex][10] = dtStatementDetails.Rows[0][0];
                            dtInvoice.Rows[invoiceRow.RowIndex][11] = dtStatementDetails.Rows[0][1];
                        }
                    }

                    Session[SessionStore.UnMatchedInvoiceListForRemitter] = dtInvoice;
                    grdInvoice.DataSource = dtInvoice;
                    grdInvoice.DataBind();
                }
            }
        }

        protected void btnConfirmMatches_Click(object sender, EventArgs e)
        {
            DataTable dtInvoiceList = (DataTable)Session[SessionStore.UnMatchedInvoiceListForRemitter];
            if (dtInvoiceList  != null)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("invoice_unique_id", typeof(System.Int32));
                dt.Columns.Add("statement_unique_id", typeof(System.Int32));
                dt.Columns.Add("invoice_number", typeof(System.String));
                dt.Columns.Add("remitter_id", typeof(System.String));
                dt.Columns.Add("amount", typeof(System.Double));
                dt.Columns.Add("ref_no_or_cheque_no", typeof(System.String));
                dt.Columns.Add("statement_amount", typeof(System.Double));

                for (int x = 0; x < dtInvoiceList .Rows.Count; x++)
                {
                    if (!string.IsNullOrEmpty(dtInvoiceList .Rows[x][1].ToString()))
                    {
                        DataRow dr = dt.NewRow();
                        dr[0] = dtInvoiceList .Rows[x][0];
                        dr[2] = dtInvoiceList .Rows[x][2];
                        dr[3] = dtInvoiceList .Rows[x][1];
                        dr[4] = dtInvoiceList.Rows[x][6];
                        dr[1] = dtInvoiceList.Rows[x][10];
                        dr[5] = dtInvoiceList.Rows[x][9];
                        dr[6] = dtInvoiceList.Rows[x][11];
                        dt.Rows.Add(dr);
                    }
                }

                DBFactory db = new DBFactory();
                db.MatchInvoicesForRemitter(((UserVO)Session["CurrentUserVO"]).customerId, ((UserVO)Session["CurrentUserVO"]).userId, dt);

                dtInvoiceList = db.GetUnmatchedInvoicesForRemitter(((UserVO)Session["CurrentUserVO"]).customerId, ((UserVO)Session["CurrentUserVO"]).userId);
                Session[SessionStore.MatchList] = dtInvoiceList ;
                grdInvoice.DataSource = dtInvoiceList;
                grdInvoice.DataBind();
            }
        }

        protected void grdInvoice_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdInvoice.DataSource = (DataTable)Session[SessionStore.UnMatchedInvoiceListForRemitter];
            grdInvoice.PageIndex = e.NewPageIndex;
            grdInvoice.DataBind();
        }

        protected void grdInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                DateTime dt;
                if (e.Row.Cells[2].Text != "" && e.Row.Cells[2].Text != "&nbsp;")
                {
                    dt = Convert.ToDateTime(e.Row.Cells[2].Text);
                    e.Row.Cells[2].Text = dt.ToString("dd-MM-yyyy");
                    e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
                }
                else
                {
                    e.Row.Cells[2].Text = "";
                }
               

                double value = 0.00;
                if (e.Row.Cells[3].Text != "" && e.Row.Cells[3].Text != "&nbsp;")
                {
                    value = Convert.ToDouble(e.Row.Cells[3].Text);
                    e.Row.Cells[3].Text = value.ToString("#,#.00", CultureInfo.InvariantCulture);
                    e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
                }
                else
                {
                    e.Row.Cells[3].Text = "0.00";
                }                

            }       
        }

    }
}